package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;

import java.io.*;

public class ArticleController {
    private static String article="./article/";
    static {
        File file=new File(article);
        if(file.isDirectory()){
            file.mkdirs();
        }
    }
    public void writeArticle(HttpRequest request,HttpResponse response){

        String q="./webapps/myweb/";
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        String author=request.getParameter("author");
        if(title==null||content==null){
            response.setEntity(new File(q+"article_fail.html"));
            return;
        }
        File file=new File(article+title+".obj");
        try(
                ObjectOutputStream oos=new ObjectOutputStream(
                        new FileOutputStream(file));
                ) {
            Article article1=new Article(title,content,author);
            oos.writeObject(article1);
        response.setEntity(new File(q+"article_success.html"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
